package com.lighting.shortlink.project.common.convention.exception;

import com.lighting.shortlink.project.common.convention.errorcode.IErrorCode;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @see ClientException
 * @see ServiceException
 * @see RemoteException
 * 抽象项目中的三类异常体系：客户端异常、服务端异常、远程服务调用异常
 * @author : lighting
 * @date : 2024/2/2 17:44
 */
@Getter
public abstract class AbstractException extends RuntimeException{
    /**
     * 异常码
     */
    public final String errorCode;

    /**
     * 异常信息
     */
    public final String errorMessage;

    protected AbstractException(String message, Throwable throwable, IErrorCode errorCode) {
        super(message,throwable);
        this.errorCode = errorCode.code();
//        如果 message 不为空，则返回 message，否则返回 errorCode.message()
        this.errorMessage = Optional.ofNullable(StringUtils.hasLength(message) ? message : null).orElse(errorCode.message());
    }
}
